<?php
namespace Users\Admin;

use \VSF\Mvc\Controller\AbstractActionController;
use \Zend\View\Model\ViewModel;

use \VSF\Form\Form;

use Users\Entity\Users;
use Users\Entity\Usersgroups;

class GroupsController extends AbstractActionController
{


	public function indexAction()
	{
		$viewParams = array();
		/* */
		$listGroups =  $this->getEntityManager()->getRepository('Users\Entity\Usersgroups')->findAll();
		$viewParams['groups'] = $listGroups;
		
		$viewModel = new ViewModel($viewParams);
		$viewModel->setTemplate('admin/groups/index');
		return $viewModel;
	}

	public function addAction()
	{
		$form = new Form(include __DIR__.'/../Form/Group.php');

		$request = $this->getRequest();
		if ($request->isPost()) {
			$group = new Usersgroups();
				
			$form->setData($request->getPost());
			if ($form->isValid()) {

				$formData = $form->getData();

				$this->populateEntity($group, $formData);
				$this->getEntityManager()->persist($group);
				$this->getEntityManager()->flush();

					
				return $this->redirect()->toRoute('admin', array(
						'module' => 'users',
						'controller' => 'groups',
						'action'     => 'index',
				));

			}
		}

		$viewParams = array('form' => $form);
		$viewModel = new ViewModel($viewParams);
		$viewModel->setTemplate('admin/groups/add');

		return $viewModel;
	}

	public function editAction()
	{
		$id = (int)$this->getEvent()->getRouteMatch()->getParam('id');
		if (!$id) {
			return $this->redirect()->toRoute('users', array('action'=>'add'));
		}
		$user = $this->getEntityManager()->find('Users\Entity\Users', $id);

		$form = new UserForm();
		$form->setBindOnValidate(false);
		$form->bind($user);
		$form->get('submit')->setAttribute('label', 'Edit');

		$request = $this->getRequest();
		if ($request->isPost()) {
			$form->setData($request->post());
			if ($form->isValid()) {
				$form->bindValues();
				$this->getEntityManager()->flush();

				// Redirect to list of users
				// Redirect to list of users
				return $this->redirect()->toRoute('admin', array(
						'controller' => 'users',
						'action'     => 'index',
				));
			}
		}

		$viewParams = array(
				'id' => $id,
				'form' => $form,
		);
		$viewModel = new ViewModel($viewParams);
		$viewModel->setTemplate('admin/users/add');

		return $viewModel;
	}

	public function deleteAction()
	{
		$id = (int)$this->getEvent()->getRouteMatch()->getParam('id');
		if (!$id) {
			// Redirect to list of users
			return $this->redirect()->toRoute('admin', array(
					'controller' => 'users',
					'action'     => 'index',
			));
		}

		$request = $this->getRequest();
		if ($request->isPost()) {
			$del = $request->post()->get('del', 'No');
			if ($del == 'Yes') {
				$id = (int)$request->post()->get('id');
				$user = $this->getEntityManager()->find('Users\Entity\Users', $id);
				if ($user) {
					$this->getEntityManager()->remove($user);
					$this->getEntityManager()->flush();
				}
			}

			// Redirect to list of users
			return $this->redirect()->toRoute('admin', array(
					'controller' => 'users',
					'action'     => 'index',
			));
		}

		$viewParams = array(
				'id' => $id,
				'user' => $this->getEntityManager()->find('Users\Entity\Users', $id)->getArrayCopy()
		);
		$viewModel = new ViewModel($viewParams);
		$viewModel->setTemplate('admin/users/add');

		return $viewModel;
	}

	/**
	 * (non-PHPdoc)
	 * @see VSF\Mvc\Controller.AbstractActionController::populateEntity()
	 */
	protected function populateEntity($entity, $data)
	{
		//\Zend\Debug\Debug::dump($data);
		parent::populateEntity($entity, $data);
		$entity->active = 1;
	}
}